//
//  ZHSelectClassifyViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2018/1/2.
//  Copyright © 2018年 greenbuilding. All rights reserved.
//

#import "ZHSelectClassifyViewController.h"

#import "ZHSelectClassifyHeaderView.h"
#import "ZHSelectClassifyTableViewCell.h"

#import "ZHSelectClassify.h"
#import "ZHSelectClassifyChild.h"

static NSString *const cellID = @"SelectClassify";

@interface ZHSelectClassifyViewController ()<UITableViewDelegate,UITableViewDataSource,ZHSelectClassifyHeaderViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//数据
@property (nonatomic,strong) NSMutableArray *dataList;

@end

@implementation ZHSelectClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"重置" target:self action: @selector(rightAction)];
}

- (NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
        
        NSString *str = @"傻逼了吧";
        
        NSMutableArray *child = [NSMutableArray array];
        for (int i = 0; i < 5; i++) {
            NSString *name = [str substringFromIndex:arc4random()%str.length];
            NSDictionary *dict = @{@"title":name};
            [child addObject:dict];
        }
        
        NSDictionary *dic = @{@"title":@"一级分类",@"child":child};
        
        NSArray *data = @[dic,dic,dic,dic,dic];
        
        _dataList = [ZHSelectClassify mj_objectArrayWithKeyValuesArray:data];
        
    }
    return _dataList;
    
}

#pragma mark <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ZHSelectClassify *classfy = self.dataList[section];
    
    return classfy.child.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZHSelectClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZHSelectClassifyTableViewCell class]) owner:nil options:nil] firstObject];
    }
    ZHSelectClassify *chassify = self.dataList[indexPath.section];
    cell.child = chassify.child[indexPath.row];
    cell.lineH.constant = chassify.isShow ? 1 : 0;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZHSelectClassify *classify = self.dataList[indexPath.section];
    
    return classify.isShow ? 44 : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    ZHSelectClassifyHeaderView *header = [ZHSelectClassifyHeaderView initWithXib];
    header.autoresizingMask = UIViewAutoresizingNone;
    header.delegate = self;
    header.section = section;
    header.classify = self.dataList[section];
    
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZHSelectClassify *classify = self.dataList[indexPath.section];
    
    ZHSelectClassifyChild *child = classify.child[indexPath.row];
    child.isSel = !child.isSel;
    
    NSInteger num = 0;
    for (ZHSelectClassifyChild *model in classify.child) {
        if (model.isSel) {
            num ++;
        }
    }
    if (num > 0) {
        classify.isSel = YES;
    }else{
        classify.isSel = NO;
    }

     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark <ZHSelectClassifyHeaderViewDelegate>
- (void)selectClassifyHeaderView:(ZHSelectClassifyHeaderView *)headerView didSelectedeSelectedAllButton:(UIButton *)sender{
    
    ZHSelectClassify *classify = self.dataList[sender.tag];
    classify.isSel = !classify.isSel;
    
    for (ZHSelectClassifyChild *child in classify.child) {
        child.isSel = classify.isSel;
    }
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sender.tag];

    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];

}

-(void) selectClassifyHeaderView:(ZHSelectClassifyHeaderView *)headerView didSelectedeRowButton:(UIButton *)sender{
    
    ZHSelectClassify *classify = self.dataList[sender.tag];
    classify.isShow = !classify.isShow;
    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sender.tag];
    
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}

//重置
- (void)rightAction{
    
    for (int i = 0; i<self.dataList.count; i++) {
        for (ZHSelectClassify *classify in self.dataList) {
            classify.isSel = NO;
            classify.isShow = NO;//关闭展开
            for (ZHSelectClassifyChild *child in classify.child) {
                child.isSel = NO;
            }
        }
    }
    [self.tableView reloadData];
}

@end
